package student;

import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableCellRenderer;

import secretaryGUI.createSecretaryWindow;

import basic.ClientConsole;

import com.sun.rowset.CachedRowSetImpl;

import flexibility.CachedRowSetTableModel;
import flexibility.userData;
import gui.studentScreen;

/**
 * 
 * This class is responsible to update courseTable according to student's ID and semesterID
 *
 */
public class fillCourses {
	
	private static String  year;
	private static String semesterName;
	private CachedRowSetImpl crs;
	private CachedRowSetImpl rs;
	private userData DBData = new userData();
	private userData DBData2 = new userData();
	private static ClientConsole studentConnection;
	private Integer myID;
	private static Integer  selectedSemesterID;
	private CachedRowSetTableModel data; 
	
	public fillCourses(ClientConsole conn, Integer studentID) {
		studentConnection=conn;
		myID=studentID;
	}
		
	public void setSemesterNameandYear(String str){
		semesterName=str.substring(0, 4);
		year=str.substring(5);
	}
	
	/**
	 * This method sets semester ID according to year and semesterName
	 * @return - semester ID
	 */
	public void setSelectedSemesterID(){
		DBData.deleteAll();
		DBData.addData(50);
		DBData.addData(1);
		DBData.addData(year);
		DBData.addData(semesterName);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		
		try {
			crs.next();
			selectedSemesterID=crs.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public Integer getSelectedSemesterID(){
		return selectedSemesterID;
	}
	
	/**
	 * This method returns all student's courses during specific semester
	 * @param myID - student's ID
	 * @param semesterID - semester ID
	 * @return - crs - all student's courses during specific semester as courseID and CourseName
	 */
	public CachedRowSetImpl getCourses(){
		
		DBData2.deleteAll();
		DBData2.addData(51);
		DBData2.addData(1);
		DBData2.addData(myID);
		DBData2.addData(getSelectedSemesterID());
		DBData2.addData(getSelectedSemesterID());
		studentConnection.acceptMsg(DBData2);
		crs = studentConnection.getResults();
		
		return crs;
	}
	
	public void update(studentScreen ssc, CachedRowSetImpl crs ){
		try {
			
			
			ssc.tableUp.setModel(data = new CachedRowSetTableModel(crs));
			DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
			centerRenderer.setHorizontalAlignment(JLabel.CENTER);
			System.out.println("Number of columns: "+ssc.tableUp.getColumnCount());
			for (int i = 0; i < ssc.tableUp.getColumnCount(); i++){
				ssc.tableUp.getColumnModel().getColumn(i)
						.setCellRenderer(centerRenderer);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
